package ugc

import (
	"sensitives/service/dic"
	"strings"
)

type common struct {
	SensWords []sensitive
}

type sensitive struct {
	Word  []string `json:"word"`
	Level uint8    `json:"level"`
	Type  string   `json:"type"`
}

func (sens *common) filter(text, types string, userDic *dic.TrieTree) {
	texts := strings.Split(text, "")
	textsLen := len(texts)
	for i := 0; i < textsLen; i++ {
		tmpText := texts[i]
		TempMap := userDic
		if _, ok := TempMap.Node[tmpText]; !ok {
			continue
		}
		TempMap = TempMap.Node[tmpText]
		if TempMap.IsEnd {
			sensi := sensitive{Word: []string{"", tmpText, ""}, Type: types, Level: TempMap.Level}
			sens.SensWords = append(sens.SensWords, sensi)
		}
		for j := i + 1; j < textsLen; j++ {
			jtext := texts[j]
			Tmpj, ok := TempMap.Node[jtext]
			if !ok {
				break
			}
			if ok && Tmpj.IsEnd {
				tmpText += jtext
				sensi := sensitive{
					Word:  []string{"", tmpText, ""},
					Type:  types,
					Level: Tmpj.Level,
				}
				sens.SensWords = append(sens.SensWords, sensi)
				TempMap = Tmpj
			}
		}
	}
}
